var jweixin = require('jweixin-module')
import app from '../App.vue';
import cookie from "@/utils/cookie";

export default {
	//判断是否在微信中  
	isWechat: function() {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			// console.log(‘是微信客户端‘)
			return true;
		} else {
			// console.log(‘不是微信客户端‘)
			return false;
		}
	},
	//刷新分享内容
	refreshShare: function() {
		if (this.isWechat()) {
			this.share({
				title: '贝客数据',
				url: window.location.href.split('#')[0],
				desc: '0门槛24小时赚钱\n一个渠道流量分享赚钱工具',
				img: 'https://oss.3yakj.com/bek/icon/16534608818009PLGdOtTjuI.png',
			});
		}
	},
	//初始化sdk配置  
	initJssdkShare: function(callback, url) {
		uni.request({
			url: app.globalData.baseURL + 'Article/getJsSdkConf',
			method: 'GET',
			header: {
				'token': cookie.get('token') ? cookie.get('token') : '',
				'time': Date.parse(new Date())
			},
			data: {
				url: url,
				appid: cookie.get('appid')
			}, //这里不是统一的，看你们后端
			success: (res) => {
				console.log(res)

				if (res.statusCode == 200) {
					let result = res.data.conf
					jweixin.config({
						debug: false,
						appId: result.appId,
						timestamp: result.timestamp,
						nonceStr: result.nonceStr,
						signature: result.signature,
						jsApiList: [
							'checkJsApi',
							'onMenuShareAppMessage',
							'onMenuShareTimeline',
							'updateAppMessageShareData',
							'updateTimelineShareData'
						]
					});
					//配置完成后，再执行分享等功能  
					if (callback) {
						callback(result);
					}
				}
			}
		});



	},
	// 分享时通知后台增加次数
	shareNumber: function(id){
		uni.request({
			url: app.globalData.baseURL + 'Article/share',
			method: 'GET',
			header: {
				'token': cookie.get('token') ? cookie.get('token') : '',
				'time': Date.parse(new Date())
			},
			data: {
				id: id
			},
			success: (res) => {}
		});
	},
	//在需要自定义分享的页面中调用  
	share: function(data) {
		data.url = data.url ? data.url : window.location.href;
		// console.log("data.url:" + data.url)
		if (!this.isWechat()) {
			return;
		}
		//每次都需要重新初始化配置，才可以进行分享  
		this.initJssdkShare(function(signData) {
			jweixin.ready(function() {
				var shareData = {
					title: data && data.title ? data.title : signData.site_name,
					desc: data && data.desc ? data.desc : signData.site_description,
					link: data.url,
					imgUrl: data && data.img ? data.img : signData.site_logo,
					type: data && data.type ? data.type : '',
					dataUrl: data && data.dataUrl ? data.dataUrl : '',
					success: function(res) {
						// 分享后的一些操作,比如分享统计等等
					},
					cancel: function(res) {}
				};
				if(jweixin.updateAppMessageShareData) {
					//分享给朋友接口
				 	jweixin.updateAppMessageShareData(shareData)
					//分享到朋友圈接口  
					jweixin.updateTimelineShareData(shareData)
				} else {
					//分享给朋友接口
					jweixin.onMenuShareAppMessage(shareData)
					//分享到朋友圈接口  
					jweixin.onMenuShareTimeline(shareData)
				}
			});
		}, data.url);
	},
}
